package com.tjetc.service.impl;

import com.tjetc.dao.OrderDao;
import com.tjetc.dao.impl.OrderDaoImpl;
import com.tjetc.domain.Order;
import com.tjetc.service.OrderService;
import com.tjetc.utils.JedisDb;
import com.tjetc.utils.PageInfo;
import redis.clients.jedis.Jedis;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private Jedis jedis = JedisDb.jedis();
    @Override
    public Order listDocOrder(Date date, String time, int did) {
        try {
            return orderDao.listDocOrder(date, time, did);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addOrder(Order order) {
        try {//考虑线程安全问题,模拟多人预约同一时间导致的问题
            String key = order.getDate()+order.getTime();
            //使用SETNX命令设置间的值,仅当键不存在时才能添加预约
            Long result = jedis.setnx(key, "1");
            System.out.println("result = " + result);
            if (result == 1){//键不存在
                orderDao.addOrder(order);
                //设置过期时间
                jedis.expire(key,5L);
                return true;
            }else{//键存在
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Order> listMyOrder(PageInfo<Order> pageInfo, int did,int status) {
        try {
            return orderDao.listMyOrder(pageInfo, did,status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(int did,int status) {
        try {
            return orderDao.getCount(did,status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Order> listMyOrderTime(int did, Date date) {
        try {
            List<Order> list = orderDao.listMyOrderTime(did, date);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public Order findByDid(int id) {
        try {
            return orderDao.findByDid(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean listInOrder(int did, Date date, String time) {
        try {
            List<Order> orders = orderDao.listInOrder(did, date, time);
            for (Order order : orders) {
                if (order.getStatus() == 0){//检测到在这个时间点之前有未处理的预约，提醒它依次就诊
                    return false;
                }
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Order> listUserOrder(int uid) {
        try {
            return orderDao.listUserOrder(uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
